/*
 * =============================================================================
 *
 *       Filename:  1108.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  09/22/2010 05:27:02 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Richard Ma (richard_ma), richard.ma.19850509@gmail.com
 *        Company:  Feng Huli Primary School
 *
 * =============================================================================
 */


#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>

/* 
 * ===  FUNCTION  ==============================================================
 *         Name:  main
 *  Description:  
 * =============================================================================
 */
    int
main ( int argc, char *argv[] )
{
    int status[13], x, y, cnt[3];
    int min, i;

    scanf("%d %d %d %d", &status[1], &status[2], &status[3], &status[10]);
    scanf("%d %d %d %d", &status[4], &status[5], &status[6], &status[11]);
    scanf("%d %d %d", &status[7], &status[8], &status[9]);
    scanf("%d %d", &status[12], &status[0]);
    scanf("%d %d", &x, &y);

    /* cnt[0] */
    if (0 == y/10) {
        if (1 == status[y]) {
            cnt[0] = 1;
        } else {
            cnt[0] = 100;
        }
    } else {
        if (1 == status[12] && 1 == status[y/10] && 1 == status[y%10]) {
            cnt[0] = 3;
        } else {
            cnt[0] = 100;
        }
    }

    /* cnt[1] */
    if (1 == status[10]) {
        if (y > x) {
            cnt[1] = y - x;
        } else {
            cnt[1] = 100 - x + y;
        }
    } else {
        cnt[1] = 100;
    }

    /* cnt[2] */
    if (1 == status[11]) {
        if (y < x) {
            cnt[2] = x - y;
        } else {
            cnt[2] = 100 - y + x;
        }
    } else {
        cnt[2] = 100;
    }

    min = 100;
    for (i=0; i<3; i++) {
        if (cnt[i] < min) min = cnt[i];
    }

    if (100 > min) printf("%d\n", min);
    else printf("-1\n");

    return EXIT_SUCCESS;
}				/* ----------  end of function main  ---------- */
